package com.hgkj.cloud.base.service.impl;

import java.io.File;
import java.io.IOException;

import com.hgkj.cloud.base.domain.Thumbnail;
import com.hgkj.cloud.base.file.FileProcess;
import com.hgkj.cloud.base.service.RemoteService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class RemoteServiceImpl implements RemoteService {
	
	private static Log log = LogFactory.getLog(RemoteServiceImpl.class);
	@Value("${server.host}")
	private String serverHost;
	@Value("${server.absolute.path}")
	private String serverAbsolutePath;
	@Autowired
	private FileProcess fp;
	
	
	public String processCommonFile(String filePath, String dir) throws Exception {
		return fp.process(filePath, dir);
	}
	public String[] processCommonFile4Array(String filePath, String dir) throws Exception {
		return fp.processForArray(filePath, dir);
	}
	
	public String[] processCommonFiles(String[] filePaths,String dir) throws Exception {
		String[] urls = new String[filePaths.length];
		for(int i=0;i<urls.length;i++) {
			urls[i] = fp.process(filePaths[i], dir);
		}
		return urls;
	}

	//生成缩略图
	public String processThumbnail(Thumbnail tn, String dir) {
		
		String filename = tn.getTargetFilename();
		IMOperation op = new IMOperation();
	    op.addImage(filename);
	    
	    String raw = tn.getWidth()+"x"+tn.getHeight()+"!";//String raw = tn.getWidth()+"x"+tn.getHeight()+"!";tn.getWidth()+"%x"+tn.getHeight()+"%";
	    op.addRawArgs("-thumbnail", raw);
        //图片质量
        op.addRawArgs("-quality", "100");//缩略图的手机上显示正常，页面上不是很正常
    
	   
	    String outputfile = FilenameUtils.getFullPath(filename)+dir+"/"+FilenameUtils.getName(filename);
	    try {
			FileUtils.touch(new File(outputfile));
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	    op.addImage(outputfile);
	    ConvertCmd convert = new ConvertCmd();
	    //convert.createScript("thumbnail.sh",op);
	    try {
			convert.run(op);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IM4JavaException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    outputfile = StringUtils.replace(outputfile,serverAbsolutePath , "");
	    return serverHost+outputfile;
	}
	public String getServerHost() {
		return serverHost;
	}

	public void setServerHost(String serverHost) {
		this.serverHost = serverHost;
	}

	public String getServerAbsolutePath() {
		return serverAbsolutePath;
	}

	public void setServerAbsolutePath(String serverAbsolutePath) {
		this.serverAbsolutePath = serverAbsolutePath;
	}

	public FileProcess getFp() {
		return fp;
	}
	public void setFp(FileProcess fp) {
		this.fp = fp;
	}
	public static void main(String[] a) {
		String name="http://www.hcapp99.com/th/324.jpg";
//		System.out.println(StringUtils.replace(FilenameUtils.getFullPath(name), "http://www.hcapp99.com", ""));
//		System.out.println(FilenameUtils.getBaseName(name));
//		System.out.println(FilenameUtils.getPath(name));
//		System.out.println(FilenameUtils.getFullPathNoEndSeparator(name));
		
//		Thumbnail t = new Thumbnail();
//		t.setTargetFilename("http://www.hcapp99.com/th/324.jpg");
//		t.setHeight(1);
//		t.setWidth(2);
//		String outputfile = StringUtils.replace(FilenameUtils.getFullPath("http://www.hcapp99.com/th/324.jpg"),"http://www.hcapp99.com/","")+"tn"+"/"+FilenameUtils.getName("http://www.hcapp99.com/th/324.jpg");
//		System.out.println(outputfile);
	}
	
}
